package uk.org.landeg.euler.problems;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

import uk.org.landeg.euler.problems.framework.AbstractProblem;
import uk.org.landeg.euler.problems.framework.ProblemLauncher;
import uk.org.landeg.euler.problems.lib.PrimeLib;

public class Problem047 extends AbstractProblem {
	public static void main(String[] args) {
		ProblemLauncher launcher = new ProblemLauncher(new Problem047());
		launcher.solve();
	}

	@Override
	public String getDescription() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer getProblemNumber() {
		// TODO Auto-generated method stub
		return 47;
	}

	@Override
	public Object solve() {
		TreeSet<Integer> primes = new TreeSet<Integer>();
		ArrayList<Set<Integer>> primesList = new ArrayList<Set<Integer>>();
		Set<Integer> theseFactors = new HashSet<Integer>();
		primes.addAll(PrimeLib.seqPrimes(500000));
		
		int max = 2;
		for (int idx = 2; idx < 1000000 ; idx++) {
			theseFactors = new HashSet<Integer>();
			for (int offset = 0 ; offset < max ; offset++) {
				theseFactors.addAll(getPrimeFactors(idx+offset,primes));
			}
			if (theseFactors.size() == max * max) {
				System.out.println("Max : " + max + "\t" + idx + "\t" + theseFactors);
				max++;
			}
			if (max > 4) {
				return idx;
			}
		}
		return null;
	}
	
	Set<Integer> getPrimeFactors  (long n, Set<Integer> primes) {
		Set<Integer> factors = new TreeSet<Integer>();
		Iterator<Integer> primeIt = primes.iterator();
		int thisFactor;
		int thisPrime;
		do {
			thisFactor = 1;
	 		thisPrime = primeIt.next();
	 		while (n % thisPrime == 0){
	 			thisFactor *= thisPrime;
	 			n /= thisPrime;
	 		}; 
	 		if (thisFactor > 1)
	 			factors.add(thisFactor);
		} while (n > 1);
		return factors;
	}
}
